<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
	public function registerAction()
	{
		require_once 'Zend/Validate.php';
		require_once 'Zend/Json.php';
		require_once 'Zend/Validate/StringLength.php';
		require_once 'Zend/Validate/Alnum.php';

		
		$validate_Alnum_Length = new Zend_Validate();
		$validate_Alnum_Length->addValidator(new Zend_Validate_StringLength(4, 16))
		               ->addValidator(new Zend_Validate_Alnum());
		               
		$validate_Length = new Zend_Validate();
		$validate_Length->addValidator(new Zend_Validate_StringLength(4, 16));
		
		$errors = array();
		
		// Validate the username
		if (!$validate_Alnum_Length->isValid($_REQUEST['username'])) 
		{
			$errors['username'] = array();
			foreach ($validate_Alnum_Length->getMessages() as $message) 
			{
		        $errors['username'][] = "$message";
		    }    
		}
		
		// Validate the password
		if (!$validate_Length->isValid($_REQUEST['password'])) 
		{
			$errors['password'] = array();
			foreach ($validate_Length->getMessages() as $message) 
			{
		        $errors['password'][] = "$message";
		    }    
		}
		if ($_REQUEST['password'] != $_REQUEST['password_ext'])
		{
			$errors['password'] = array();
			$errors['password'][] = "Passwords do not match";
		}
		
		$this->view->json = Zend_Json::encode($errors);
	}
	public function loginAction()
	{
		require_once 'Zend/Db.php';
		require_once 'Zend/Auth/Adapter/DbTable.php';
		require_once 'Zend/Debug.php';
		
    	$config = Zend_Registry::get('config');
		
		$dbAdapter = Zend_Db::factory($config->database->type, array(
		    'host'     => $config->database->host,
		    'username' => $config->database->username,
		    'password' => $config->database->password,
		    'dbname'   => $config->database->name
		));
		$profiler = $dbAdapter->getProfiler();
		
		
		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
		$authAdapter->setTableName('auth_users')
		            ->setIdentityColumn('user_name')
		            ->setCredentialColumn('user_pass');
		$authAdapter->setIdentity($_REQUEST['username'])
            		->setCredential($_REQUEST['password']);
        $result = $authAdapter->authenticate();
        
        
        if ($result->isValid()) {
        	$this->view->valid = 'true';
        }
        else
        {
        	$this->view->valid = '';
		    foreach ($result->getMessages() as $message)
		    {
		        $this->view->valid .= "$message<br/>";
		    }
        }
	}
	public function logoutAction()
	{
		require_once 'Zend/Session.php';
		Zend_Session::destroy($remove_cookie = true, $readonly = true);
		$this->_redirect('/index/login');
		
	}
    public function indexAction()
    {
		require_once 'Zend/Db.php';
		require_once 'Zend/Db/Table.php';
    	
    	/*
    	$config = Zend_Registry::get('config');
    	require_once '../application/models/AuthUsers.php';
		
		$db = Zend_Db::factory($config->database->type, array(
		    'host'     => $config->database->host,
		    'username' => $config->database->username,
		    'password' => $config->database->password,
		    'dbname'   => $config->database->name
		));
		
		$select = $db->select();
		$select->from('test');
		$stmt = $select->query();
		$result = $stmt->fetchAll();
		
		$table = new AuthUsers(array('db' => $db));

		$data = array(
		    'user_name'      => 'test',
		    'user_password'	 => 'test',
		    'user_logged'    => new Zend_Db_Expr('NOW()')
		);
		$table->insert($data);
		
		$db->closeConnection();
		*/
		
		echo $config->webhost; 
		//$this->view->id = $this->_getParam('id', 1);
		$this->view->foo = 'bar';
    }
}

?>
